package cn.gson.crm.controller.dept;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.DeptDao;
import cn.gson.crm.model.entity.Dept;

@WebServlet("/dept/delete")
public class DeptDeleteServlet extends BaseServlet{

	/**
	 *部门删除
	 */
	private static final long serialVersionUID = 1L;
	private DeptDao deptDao = new DeptDao();
	
	@Override
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String[] deletIds = request.getParameterValues("id");
		List<Dept> cantBeDeleDeptList = new ArrayList<>();
		for (String id : deletIds) {
			//判断被删除的部门是否有子部门
			List<Dept> childDepet = deptDao.getSonDept(1, id);
			if(childDepet==null){
				deptDao.deletDepartment(0, id);
			}else{
				//将不能删除的部门加入集合
				Dept depts =deptDao.getDeptById(1, id);
				cantBeDeleDeptList.add(depts);
			}
			if(cantBeDeleDeptList.size()>0){
				request.getSession().setAttribute("cantBeDeleDeptList", cantBeDeleDeptList);
				request.getSession().setAttribute( "deleErro","有子部门，不能被删除");
			}
		}
		redirect("dept");
	}
}
